#include "loginwidget.h"
#include "model/data.h"
#include "phoneloginwidget.h"
#include "registerwidget.h"
#include "model/datacenter.h"
#include <QGridLayout>
#include <QHBoxLayout>
#include <QRadioButton>
#include "toast.h"
#include "mainwidget.h"

using namespace model;

LoginWidget::LoginWidget(QWidget *parent): QWidget{parent}
{
    // 1. 设置本窗口的基本属性
    this->setFixedSize(420, 320);
    this->setWindowTitle("登录");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setStyleSheet("QWidget { background-color: rgb(255, 255, 255); }");
    this->setAttribute(Qt::WA_DeleteOnClose);

    // 2. 创建布局管理器
    QGridLayout* layout = new QGridLayout();
    layout->setSpacing(10);
    layout->setContentsMargins(50, 15, 50, 10);
    this->setLayout(layout);

    // 3. 创建标题区域（图标 + 文字）
    QWidget* titleWidget = new QWidget();
    titleWidget->setFixedHeight(50);
    QHBoxLayout* titleLayout = new QHBoxLayout(titleWidget);
    titleLayout->setContentsMargins(0, 0, 0, 5);

    // 创建图标 QLabel
    QLabel* iconLabel = new QLabel();
    QPixmap iconPixmap(":/resource/image/logo.png");
    iconPixmap = iconPixmap.scaled(42, 42, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    iconLabel->setPixmap(iconPixmap);
    iconLabel->setFixedSize(42, 42);

    // 创建文字标题
    titleLabel = new QLabel();
    titleLabel->setText("登录");
    titleLabel->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
    titleLabel->setStyleSheet("QLabel { font-size: 32px; font-weight: 600; }");

    titleLayout->addStretch(1);
    titleLayout->addWidget(iconLabel);
    titleLayout->addSpacing(10); // 图标和文字之间的间距
    titleLayout->addWidget(titleLabel);
    titleLayout->addStretch(1);

    // 4. 创建输入框
    usernameEdit = model::createLineEdit(QIcon(":/resource/image/username.png"), "输入用户名");
    passwordEdit = model::createLineEdit(QIcon(":/resource/image/password.png"), "输入密码");
    passwordEdit->setEchoMode(QLineEdit::Password); // 密码输入
    verifyCodeEdit = model::createLineEdit(QIcon(":/resource/image/verifycode.png"), "输入验证码");

    // 5. 创建显示验证码图片的控件 (此处先用 QPushButton 来表示一下, 后续进一步编写这里的逻辑)
    //    后续会自定义 QWidget, 通过画图 api 来实现这里的验证码功能.
    // QPushButton* verifyCodeWidget = new QPushButton();
    // verifyCodeWidget->setFixedHeight(40);
    // verifyCodeWidget->setText("验证码");
    // verifyCodeWidget->setStyleSheet(R"(
    //     QWidget {
    //         border-radius: 5px;
    //         background-color: #EAEAEA;
    //         padding-left: 4px;
    //         border: 1px solid transparent;
    //         font-size: 12px;
    //     }
    //     QWidget:focus {
    //         background-color: #FFFFFF;
    //         border: 1px solid #07c160;
    //     }
    // )");
    verifyCodeWidget = new VerifyCodeWidget(this);

    // 6. 创建登录按钮
    submitBtn = new QPushButton();
    submitBtn->setText("登录");
    submitBtn->setFixedHeight(40);
    submitBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    submitBtn->setStyleSheet(R"(
        QPushButton {
            border: none;
            border-radius: 12px;
            background-color: rgb(7, 193, 96);
            color: rgb(255, 255, 255);
        }
        QPushButton:pressed {
            background-color: rgb(6, 174, 86);
        }
    )");

    // 7. 创建切换到手机号登录按钮
    phoneModeBtn = new QPushButton();
    phoneModeBtn->setFixedSize(100, 40);
    phoneModeBtn->setText("手机号登录");
    phoneModeBtn->setStyleSheet(R"(
        QPushButton { border: none; border-radius: 10px; background-color: transparent; }
        QPushButton:pressed { background-color: rgb(240, 240, 240); }
    )");

    // 8. 注册
    switchModeBtn = new QPushButton();
    switchModeBtn->setFixedSize(100, 40);
    switchModeBtn->setText("注册");
    switchModeBtn->setStyleSheet(phoneModeBtn->styleSheet());

    // 9. 添加到布局管理器中
    layout->addWidget(titleWidget, 0, 0, 1, 5);
    layout->addWidget(usernameEdit, 1, 0, 1, 5);
    layout->addWidget(passwordEdit, 2, 0, 1, 5);
    layout->addWidget(verifyCodeEdit, 3, 0, 1, 4);
    layout->addWidget(verifyCodeWidget, 3, 4, 1, 1);
    layout->addWidget(submitBtn, 4, 0, 1, 5);
    layout->addWidget(phoneModeBtn, 5, 0, 1, 1);
    layout->addWidget(switchModeBtn, 5, 4, 1, 1);

    // 10. 处理信号槽
    connect(switchModeBtn, &QPushButton::clicked, this, &LoginWidget::switchMode);

    connect(phoneModeBtn, &QPushButton::clicked, this, [=]() {
        PhoneLoginWidget* phoneLoginWidget = new PhoneLoginWidget(nullptr);
        phoneLoginWidget->show();

        // 关闭当前窗口
        this->close();
    });

    connect(submitBtn, &QPushButton::clicked, this, &LoginWidget::clickSubmitBtn);
}

void LoginWidget::switchMode()
{
    if (isLoginMode) {
        // 当前是登录模式, 切换到注册模式
        this->setWindowTitle("注册");
        titleLabel->setText("注册");
        submitBtn->setText("注册");
        switchModeBtn->setText("登录");
    } else {
        // 当前是注册模式, 切换到登录模式
        this->setWindowTitle("登录");
        titleLabel->setText("登录");
        submitBtn->setText("登录");
        switchModeBtn->setText("注册");
    }
    isLoginMode = !isLoginMode;
}

void LoginWidget::clickSubmitBtn(){
    // 1. 先获取输入框内容
    const QString& username = usernameEdit->text();
    const QString& password = passwordEdit->text();
    const QString& verifyCode = verifyCodeEdit->text();
    if(username.isEmpty() || password.isEmpty() || verifyCode.isEmpty()){
        Toast::showMessage("用户名/密码/验证码不能为空!");
        return;
    }
    // 2. 对比验证码
    if(!verifyCodeWidget->checkVerifyCode(verifyCode)){
        Toast::showMessage("验证码错误, 请重新输入");
        verifyCodeWidget->refreshVerifyCode();
        return;
    }
    // 3. 网络请求
    DataCenter* dataCenter = DataCenter::getInstance();
    if(isLoginMode){
        // 登录
        connect(dataCenter, &DataCenter::userLoginDone, this, &LoginWidget::userLoginDone);
        dataCenter->userLoginAsync(username, password);
    }else{
        // 注册
        connect(dataCenter, &DataCenter::userRegisterDone, this, &LoginWidget::userRegisterDone);
        dataCenter->userRegisterAsync(username, password);
    }
}

void LoginWidget::userLoginDone(bool ok, const QString &reason){
    // 此处先判断是否登录成功.
    // 若失败, 给用户反馈失败原因.
    if (!ok) {
        Toast::showMessage("登录失败! " + reason);
        return;
    }

    // 登录成功, 需要跳转到主界面.
    MainWidget* mainWidget = MainWidget::getInstance();
    mainWidget->show();

    this->close();
}

void LoginWidget::userRegisterDone(bool ok, const QString &reason){
    if (!ok) {
        Toast::showMessage("注册失败! " + reason);
        return;
    }
    Toast::showMessage("注册成功!");

    // 切换到登录界面
    this->switchMode();

    // 输入框清空一下.
    // 主要是要清空用户名和密码, 验证码输入框的内容的.
    // 但是此处, 只清空一下验证码. 用户名密码这里的情况大概率还是同样的内容.
    verifyCodeEdit->clear();

    // 更新验证码
    verifyCodeWidget->refreshVerifyCode();
}





























